package com.playball.lottery.fragment.frm;

import com.playball.lottery.R;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.frm.yhzx.LoginActivity;
import com.playball.lottery.nt.GetJsonData;
import com.playball.lottery.util.Logger;
import com.playball.lottery.util.LotConstants;


@SuppressLint("JavascriptInterface")
public class WebFragment extends Fragment implements LotConstants {
	public static final String TAG = "WebFragment";
	
	private View _RootView;
	private WebView webView;
	private String wap_url;
	
	private ShowTipDialog showdialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		wap_url = getArguments().getString("wap_url");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		_RootView = inflater.inflate(R.layout.fragment_web, null);
		return _RootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		showdialog = new ShowTipDialog();
		
		webView = (WebView) _RootView.findViewById(R.id.web);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		webView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				showdialog.closeDialog();
			}
		});
		webView.addJavascriptInterface(new CallByWeb(), "jdd");
		
//		wap_url = GetJsonData.urlPath + "?" + "sign=8AC7480990B9E4085FCB9DA5CD39FBD0&platformVersion=4.1.2&platformCode=Android&cmdId=0&appVersion=2.1.0&cmdName=app_zz&uuid=A1000032811C55A&action=107&UserID="+ApplicationData.getInstance().getUserID()+"&params={}";
//		wap_url = "http://client.jiangduoduo.com/go/tuijian/index.html";
		
		syncCookies();
		
		webView.loadUrl(wap_url);
		showdialog.showDialog(getActivity());
	}
	
	public void syncCookies() {
		if(getActivity() != null) {
			GetJsonData.synCookies(getActivity(), wap_url, ApplicationData.getInstance().isUserLogin());
		}
	}
	
	public void onResume() {
	    super.onResume();
	     //统计页面
	}
	
	public void onPause() {
	    super.onPause();
	}
	
	public boolean canGoBack() {
		return webView.canGoBack();
	}
	
	public void goBack() {
		if(webView.canGoBack()) {
			webView.goBack();
		}
	}
	

	public class CallByWeb {
		/**
		 * 用于WAP端回调用户登录接口
		 * 
		 * js调用方法：window.jdd.doLogin();
		 */
		public void doLogin() {
			getActivity().runOnUiThread(new Runnable() {
				public void run() {
					getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
				}
			});
		}
		
//		public void callByWebView(final String type) {
//			getActivity().runOnUiThread(new Runnable() {
//				public void run() {
//					getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
//				}
//			});
//		}
		
	}

}
